package com.lagou.service.Impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.Impl.CourseContentDao;
import com.lagou.dao.Impl.CourseDao;
import com.lagou.dao.Impl.Impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpl ();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {

        List<Course_Section> sectionList = contentDao.findSectionAndLessonByCourseId (courseId);

        return sectionList;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {

        Course course = contentDao.findCourseByCourseId (courseId);
        return course;
    }

    //新建章节信息
    @Override
    public String saveSection(Course_Section section) {

        //1.补全章节信息
        section.setStatus (2);//状态 0隐藏 1待更新 2已发布
        String dateFormart = DateUtils.getDateFormart ();
        section.setCreate_time (dateFormart);
        section.setUpdate_time (dateFormart);

        //2.调用dao
        int row = contentDao.saveSection (section);

        //3.根据是否插入成功，封装对应信息
        if(row > 0) {
            //保存成功
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }


    //修改章节信息
    @Override
    public String updateSection(Course_Section section) {

        //1.补全信息
        String date = DateUtils.getDateFormart ();
        section.setUpdate_time (date);

        //2.调用dao
        int row = contentDao.updateSection (section);

        //3.判断是否插入成功
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }

    //修改章节状态
    @Override
    public String updateSectionStatus(int id, int status) {

        int row = contentDao.updateSectionStatus (id, status);

        //判断是否修改成功
        if (row > 0 ) {
            //修改状态成功
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }

    //保存课时信息
    @Override
    public String saveLesson(Course_Lesson lesson) {

        //补全信息
        lesson.setStatus (2);//0-隐藏，1-未发布，2-已发布
        String dateFormart = DateUtils.getDateFormart ();
        lesson.setCreate_time (dateFormart);
        lesson.setUpdate_time (dateFormart);

        //调用dao
        int row = contentDao.saveLesson (lesson);

        //判断是否成功
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }

    //修改课时状态
    @Override
    public String updateLesson(Course_Lesson lesson) {

        //1.补全信息
        String dateFormart = DateUtils.getDateFormart ();
        lesson.setUpdate_time (dateFormart);

        //2.调用dao
        int row = contentDao.updateLesson (lesson);

        //3.判断是否插入成功
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }

    //修改课时状态
    @Override
    public String upadateLessonStatus(int id, int status) {

        int row = contentDao.updateLessonStatus (id, status);
        //判断是否修改成功
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString ();
            return result;
        } else {
            String result = StatusCode.FATL.toString ();
            return result;
        }
    }


}
